package com.huluobo.a2107mvpimdemo.login.presenter;

import com.huluobo.a2107mvpimdemo.App;
import com.huluobo.a2107mvpimdemo.base.BasePresenter;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

/**
 * Created by LC on 2023/9/20.
 */
public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {
    @Override
    public void login(String username, String password) {
        //匹配正则
        if (username.matches(App.REGEX_USERNAME)) {
            if (password.matches(App.REGEX_PASSWORD)) {
                if (isAttach()) {
                    getView().onStartLogin();
                    //登录IM
                    loginEaseMob(username, password);
                }
            } else {
                //密码不匹配
                getView().onPasswordError();
            }
        } else {
            //用户名不匹配
            getView().onUsernameError();
        }
    }

    //相当于M层的方法,可以提取为一个M层,但是没必要
    private void loginEaseMob(String username, String password) {
        EMClient.getInstance().login(username, password, new EMCallBack() {
            @Override
            public void onSuccess() {
                getView().onLoginSuccess();
            }

            @Override
            public void onError(int code, String error) {
                getView().onLoginFailed(error);
            }
        });
    }
}
